﻿Public Class Form1
  Const BaseKey As String = "Software\Avian Waves\BattLineSvc"

  Private _lastY As Integer = 0
  Private _delta As Integer = 0

  Private _colorFGN As Color
  Private _colorBGN As Color
  Private _colorFGL As Color
  Private _colorBGL As Color
  Private _colorFGC As Color
  Private _colorBGC As Color
  Private _colorFGA As Color
  Private _colorBGA As Color
  Private _colorLeftLine As Color
  Private _colorRightLine As Color
  Private _colorBottomLine As Color

  Private _hasApplied As Boolean = False

  Private Declare Function ActivateService Lib "coredll.dll" (ByVal lpszDevKey As String, ByVal dwClientInfo As Integer) As IntPtr
  Private Declare Function GetServiceHandle Lib "coredll.dll" (ByVal szPrefix As String, ByVal szDllName As System.Text.StringBuilder, ByVal pdwDllBuf As Integer) As IntPtr
  Private Declare Function DeregisterService Lib "coredll.dll" (ByVal hDevice As IntPtr) As Boolean


  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.LoadFromRegistry()
  End Sub




  Private Sub LoadFromRegistry()
    Me.NumericUpDownInterval.Value = GetRegValueInt("TimerIntervalSeconds", 0)
    Me.NumericUpDownHeight.Value = GetRegValueInt("Height", 2)
    Me.NumericUpDownLeftOffset.Value = GetRegValueInt("LeftOffset", 0)
    Me.NumericUpDownRightOffset.Value = GetRegValueInt("RightOffset", 0)
    Me.NumericUpDownLow.Value = GetRegValueInt("LowPercent", 30)
    Me.NumericUpDownCritical.Value = GetRegValueInt("CriticalPercent", 15)
    Me.CheckBoxShowLeftLine.Checked = GetRegValueBool("ShowLeftLine", False)
    Me.CheckBoxShowRightLine.Checked = GetRegValueBool("ShowRightLine", False)
    Me.CheckBoxShowBottomLine.Checked = GetRegValueBool("ShowBottomLine", False)

    Me._colorFGN = FromRGB(GetRegValueInt("ColorForegroundSolidNormal", ToRGB(0, 180, 0)))
    Me._colorBGN = FromRGB(GetRegValueInt("ColorBackgroundSolidNormal", ToRGB(0, 75, 0)))
    Me._colorFGL = FromRGB(GetRegValueInt("ColorForegroundSolidLow", ToRGB(180, 180, 0)))
    Me._colorBGL = FromRGB(GetRegValueInt("ColorBackgroundSolidLow", ToRGB(75, 75, 0)))
    Me._colorFGC = FromRGB(GetRegValueInt("ColorForegroundSolidCritical", ToRGB(180, 0, 0)))
    Me._colorBGC = FromRGB(GetRegValueInt("ColorBackgroundSolidCritical", ToRGB(75, 0, 0)))
    Me._colorFGA = FromRGB(GetRegValueInt("ColorForegroundSolidCharging", ToRGB(140, 140, 180)))
    Me._colorBGA = FromRGB(GetRegValueInt("ColorBackgroundSolidCharging", ToRGB(55, 55, 75)))
    Me._colorLeftLine = FromRGB(GetRegValueInt("ColorLeftLine", ToRGB(0, 0, 0)))
    Me._colorRightLine = FromRGB(GetRegValueInt("ColorRightLine", ToRGB(0, 0, 0)))
    Me._colorBottomLine = FromRGB(GetRegValueInt("ColorBottomLine", ToRGB(0, 0, 0)))
  End Sub

  Private Sub SaveToRegistry()
    SetRegValueInt("TimerIntervalSeconds", Me.NumericUpDownInterval.Value)
    SetRegValueInt("Height", Me.NumericUpDownHeight.Value)
    SetRegValueInt("LeftOffset", Me.NumericUpDownLeftOffset.Value)
    SetRegValueInt("RightOffset", Me.NumericUpDownRightOffset.Value)
    SetRegValueInt("LowPercent", Me.NumericUpDownLow.Value)
    SetRegValueInt("CriticalPercent", Me.NumericUpDownCritical.Value)
    SetRegValueBool("ShowLeftLine", Me.CheckBoxShowLeftLine.Checked)
    SetRegValueBool("ShowRightLine", Me.CheckBoxShowRightLine.Checked)
    SetRegValueBool("ShowBottomLine", Me.CheckBoxShowBottomLine.Checked)


    SetRegValueInt("ColorForegroundSolidNormal", ToRGB(Me._colorFGN))
    SetRegValueInt("ColorBackgroundSolidNormal", ToRGB(Me._colorBGN))
    SetRegValueInt("ColorForegroundSolidLow", ToRGB(Me._colorFGL))
    SetRegValueInt("ColorBackgroundSolidLow", ToRGB(Me._colorBGL))
    SetRegValueInt("ColorForegroundSolidCritical", ToRGB(Me._colorFGC))
    SetRegValueInt("ColorBackgroundSolidCritical", ToRGB(Me._colorBGC))
    SetRegValueInt("ColorForegroundSolidCharging", ToRGB(Me._colorFGA))
    SetRegValueInt("ColorBackgroundSolidCharging", ToRGB(Me._colorBGA))
    SetRegValueInt("ColorLeftLine", ToRGB(Me._colorLeftLine))
    SetRegValueInt("ColorRightLine", ToRGB(Me._colorRightLine))
    SetRegValueInt("ColorBottomLine", ToRGB(Me._colorBottomLine))

    ' Tickle the monitored registry location to let battlinesvc know we changed the config
    'SetRegValueInt("_ConfigChanged", GetRegValueInt("_ConfigChanged", 0) + 1)
  End Sub







  Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles _
   MyBase.MouseDown, _
   Label1.MouseDown, _
   Label2.MouseDown, _
   Label3.MouseDown, Label4.MouseDown, Label5.MouseDown, Label6.MouseDown, Label7.MouseDown
    '''''''''''''''
    Me._lastY = e.Y
    Me._delta = 0
  End Sub

  Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles _
   MyBase.MouseMove, _
   MyBase.MouseUp, _
   Label1.MouseMove, _
   Label1.MouseUp, _
   Label2.MouseMove, _
   Label2.MouseUp, _
   Label3.MouseMove, _
   Label3.MouseUp, Label4.MouseUp, Label4.MouseMove, Label5.MouseUp, Label5.MouseMove, Label6.MouseUp, Label6.MouseMove, Label7.MouseUp, Label7.MouseMove
    ''''''''''''''''''''''''''''''
    Me._delta += (e.Y - Me._lastY)
    Me._lastY = e.Y
    Me.TimerScroll.Enabled = True
  End Sub

  Private Sub TimerScroll_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerScroll.Tick
    Me.TimerScroll.Enabled = False
    Me.AutoScrollPosition = New Point(Me.AutoScrollPosition.X, -Me.AutoScrollPosition.Y - Me._delta)
    Me._delta = 0
  End Sub




  Private Function GetRegValueBool(ByVal ValueName As String, ByVal DefaultValue As Boolean) As Boolean
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As Integer = Integer.Parse(.GetValue(ValueName).ToString)
            If x = 0 Then
              Return False
            ElseIf x <> 1 Then
              .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
            End If
            Return True
          Catch
          End Try
        End If

        If DefaultValue Then
          .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
        Else
          .SetValue(ValueName, 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If
        .Close()
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Private Sub SetRegValueBool(ByVal ValueName As String, ByVal Value As Boolean)
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        If Value Then
          .SetValue(ValueName, 1, Microsoft.Win32.RegistryValueKind.DWord)
        Else
          .SetValue(ValueName, 0, Microsoft.Win32.RegistryValueKind.DWord)
        End If
        .Close()
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

  Private Function GetRegValueInt(ByVal ValueName As String, ByVal DefaultValue As Integer) As Integer
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        If .GetValue(ValueName) IsNot Nothing Then
          Try
            Dim x As Integer = Integer.Parse(.GetValue(ValueName).ToString)
            Return x
          Catch
          End Try
        End If

        .SetValue(ValueName, DefaultValue, Microsoft.Win32.RegistryValueKind.DWord)
        .Close()
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [GET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try

    Return DefaultValue
  End Function

  Private Sub SetRegValueInt(ByVal ValueName As String, ByVal Value As Integer)
    Try
      With Microsoft.Win32.Registry.CurrentUser.CreateSubKey(BaseKey)
        .SetValue(ValueName, Value, Microsoft.Win32.RegistryValueKind.DWord)
        .Close()
      End With
    Catch ex As Exception
      Throw New Exception("Unable to open registry [SET " & ValueName & "].  Error: " & ex.Message, ex)
    End Try
  End Sub

  Private Function ToRGB(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As Integer
    Return CInt(R) Or (CInt(G) << 8) Or (CInt(B) << 16)
  End Function

  Private Function ToRGB(ByVal c As Color) As Integer
    Return ToRGB(c.R, c.G, c.B)
  End Function

  Private Function FromRGB(ByVal c As Integer) As Color
    Dim r As Integer = c And &HFF
    Dim g As Integer = (c >> 8) And &HFF
    Dim b As Integer = (c >> 16) And &HFF
    Return Color.FromArgb(r, g, b)
  End Function

  Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
    Me.SaveToRegistry()

    Windows.Forms.Cursor.Current = Cursors.WaitCursor
    Dim svc As IntPtr = GetServiceHandle("BLS0:", Nothing, 0)
    If svc <> IntPtr.Zero Then
      If DeregisterService(svc) Then
        ActivateService("BattLineSvc", 0)
      End If
    End If
    Windows.Forms.Cursor.Current = Cursors.Default

    Me._hasApplied = True
  End Sub

  Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
    If Me._hasApplied Then
      MessageBox.Show("If you notice that other apps that live in the taskbar (such as HTC Notification Manager, Q-Menu, X-Task, etc.) have ceased to function after applying changes, soft reset your device.", "BattLineSvc")
    End If
    Me.Close()
  End Sub

  Private Sub ButtonSetNF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetNF.Click
    Dim cs As New ColorSelector(Me._colorFGN)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorFGN = cs.SelectedColor
    End If
  End Sub
  Private Sub ButtonSetNB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetNB.Click
    Dim cs As New ColorSelector(Me._colorBGN)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorBGN = cs.SelectedColor
    End If
  End Sub

  Private Sub ButtonSetLF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLF.Click
    Dim cs As New ColorSelector(Me._colorFGL)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorFGL = cs.SelectedColor
    End If
  End Sub
  Private Sub ButtonSetLB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLB.Click
    Dim cs As New ColorSelector(Me._colorBGL)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorBGL = cs.SelectedColor
    End If
  End Sub

  Private Sub ButtonSetCF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCF.Click
    Dim cs As New ColorSelector(Me._colorFGC)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorFGC = cs.SelectedColor
    End If
  End Sub
  Private Sub ButtonSetCB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCB.Click
    Dim cs As New ColorSelector(Me._colorBGC)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorBGC = cs.SelectedColor
    End If
  End Sub

  Private Sub ButtonSetAF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAF.Click
    Dim cs As New ColorSelector(Me._colorFGA)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorFGA = cs.SelectedColor
    End If
  End Sub
  Private Sub ButtonSetAB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAB.Click
    Dim cs As New ColorSelector(Me._colorBGA)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorBGA = cs.SelectedColor
    End If
  End Sub

  Private Sub ButtonSetLeftLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetLeftLine.Click
    Dim cs As New ColorSelector(Me._colorLeftLine)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorLeftLine = cs.SelectedColor
    End If
  End Sub
  Private Sub ButtonSetRightLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetRightLine.Click
    Dim cs As New ColorSelector(Me._colorRightLine)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorRightLine = cs.SelectedColor
    End If
  End Sub
  Private Sub ButtonSetBottomLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetBottomLine.Click
    Dim cs As New ColorSelector(Me._colorBottomLine)
    If cs.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._colorBottomLine = cs.SelectedColor
    End If
  End Sub



End Class


